import { defineConfig, loadEnv } from "vite";
import createVitePlugins from "./vite/plugins";
const path = require("path");

// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    base: "./",
    // 开发服务器选项 https://cn.vitejs.dev/config/#server-options
    server: {
      open: true,
      proxy: {
        "/proxy": {
          target: env.VITE_APP_API_BASEURL,
          changeOrigin: command === "serve" && env.VITE_OPEN_PROXY == "true",
          rewrite: (path) => path.replace(/\/proxy/, ""),
        },
      },
    },
    // 构建选项 https://cn.vitejs.dev/config/#server-fsserve-root
    build: {
      outDir: mode == "production" ? "dist" : `dist-${mode}`,
      sourcemap: env.VITE_BUILD_SOURCEMAP == "true",
      terserOptions: {
        compress: {
          drop_console: env.VITE_BUILD_DROP_CONSOLE == "true",
        },
      },
    },
    plugins: createVitePlugins(env, command === "build"),
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "src/assets/styles/utils.scss" as *;
            @use "src/assets/styles/variables.scss" as *;
          `,
        },
      },
    },
  });
};
